# -*- coding: utf-8 -*-

from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.core import urlresolvers


"""
    El Team es creado por un usuario.
    Cualquier Player que haya participado en el Team, puede volver a usarlo en otra partida.
"""
class Team(models.Model):

    name = models.CharField(verbose_name=_(u'Team name'), max_length=200, blank=False, null=False, unique=True)

    description = models.TextField(verbose_name=_(u'Team description'), max_length=2000, blank=True)

    is_dead = models.BooleanField(verbose_name=_(u'Is dead?'), blank=False, null=False, default=False)

    #image

    date_created  = models.DateTimeField(null=True, auto_now_add=True)

    date_updated  = models.DateTimeField(null=True, auto_now=True)

    class Meta:
        app_label = 'game'
        verbose_name = _(u'Team')
        verbose_name_plural = _(u'Teams')

    def __unicode__(self):
        return unicode(self.id) + " -- " + unicode(self.name)

    def get_growth_statistics(self):
        growth_statistics = 0
        for p in players:
            growth_statics += p.get_growth_statistics()
        growth_statistics = growth_statistics / len(players)
        return growth_statistics

    def get_kill_statistics(self):
        kill_points = 0
        for p in players:
            kill_points += p.kill_points
        kill_points = kill_points / len(players)
        return kill_points

    def is_game_over(self):
        if self.ants.count() == 0 and resources == 0:
            return True
        else:
            return False

    def game_over(self):
        statistic = self.user_profile.statistics.get(status = 'P')
        statistic.num_deads = self.kill_points
        self.is_dead = True
        self.team.game_over()

    def is_team_over(self):
        for player in self.players.all():
            if player.is_dead == False:
                return False
        return True

    def team_over(self):
        self.is_dead = True
        for player in self.players:
            statistic = player.user_profile.statistics.get(status='P')
            statistic.games += 1
            statistic.games_lose += 1
        self.game.game_over()

    """
        ¿Qué UserProfiles han participado en éste equipo?
        Habrá que pensar cuando se hace pública ésta información (quizás al finalizar la semana).
    """
    """
    def who_has_been_involved:
        set_users = Set()
        for p in players:
            set_users.push(p.user_profile)
        return set_users
    """
